package com.easemob.chat;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import com.easemob.util.EMLog;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.RosterStorage;
import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;

class RosterStorageImpl
  implements RosterStorage
{
  private static final String TAG = "rosterstorage";
  private static final String PERF_KEY_ROSTERVER = "easemob.roster.ver.";
  private String version;
  private EMContactManager contactManager;
  private Context appContext;
  private ArrayList<RosterPacket.Item> rosterItems;

  public RosterStorageImpl(Context paramContext, EMContactManager paramEMContactManager)
  {
    this.appContext = paramContext;
    this.contactManager = paramEMContactManager;
    this.version = null;
    this.rosterItems = new ArrayList();
  }

  void loadEntries()
  {
    Iterator localIterator = this.contactManager.contactTable.values().iterator();
    while (localIterator.hasNext())
    {
      EMContact localEMContact = (EMContact)localIterator.next();
      EMContactManager.getInstance();
      RosterPacket.Item localItem = new RosterPacket.Item(EMContactManager.getEidFromUserName(localEMContact.username), localEMContact.username);
      localItem.setItemType(RosterPacket.ItemType.both);
      this.rosterItems.add(localItem);
    }
    EMLog.d("rosterstorage", "roster storage load entries, roster items size:" + this.rosterItems.size());
  }

  public void addEntry(RosterPacket.Item paramItem, String paramString)
  {
    if ((paramItem.getItemType() == RosterPacket.ItemType.both) || (paramItem.getItemType() == RosterPacket.ItemType.from))
    {
      EMLog.d("rosterstorage", "roster storage add new contact:" + paramItem.getUser());
      String str1 = paramItem.getUser();
      String str2 = EMContactManager.getUserNameFromEid(str1);
      String str3 = EMContactManager.getBareEidFromUserName(str2);
      EMContactManager.getInstance().addContactInternal(new EMContact(str3, str2));
    }
    if ((paramString != null) && (!paramString.equals("")) && (!paramString.equals(this.version)))
      updateRosterVersion(paramString);
  }

  public List<RosterPacket.Item> getEntries()
  {
    return this.rosterItems;
  }

  public RosterPacket.Item getEntry(String paramString)
  {
    Iterator localIterator = this.rosterItems.iterator();
    while (localIterator.hasNext())
    {
      RosterPacket.Item localItem = (RosterPacket.Item)localIterator.next();
      if (localItem.getName().equals(paramString))
        return localItem;
    }
    EMLog.e("rosterstorage", "cant find roster entry for jid:" + paramString);
    return null;
  }

  public int getEntryCount()
  {
    EMLog.d("rosterstorage", "get entry count return:" + this.rosterItems.size());
    return this.rosterItems.size();
  }

  public String getRosterVersion()
  {
    if (this.version == null)
    {
      this.version = PreferenceManager.getDefaultSharedPreferences(this.appContext).getString("easemob.roster.ver." + EMSessionManager.getInstance().currentUser.eid, "");
      EMLog.d("rosterstorage", "load roster storage for jid" + EMSessionManager.getInstance().currentUser.eid + " version:" + this.version);
    }
    return this.version;
  }

  private void updateRosterVersion(String paramString)
  {
    this.version = paramString;
    SharedPreferences.Editor localEditor = PreferenceManager.getDefaultSharedPreferences(this.appContext).edit();
    localEditor.putString("easemob.roster.ver." + EMSessionManager.getInstance().currentUser.eid, paramString);
    localEditor.commit();
    EMLog.d("rosterstorage", "updated roster version to:" + paramString);
  }

  public void removeEntry(String paramString1, String paramString2)
  {
    String str = EMContactManager.getUserNameFromEid(paramString1);
    EMContactManager.getInstance().deleteContactInternal(str);
    if ((paramString2 != null) && (!paramString2.equals("")) && (!paramString2.equals(this.version)))
      updateRosterVersion(paramString2);
  }

  public void updateLocalEntry(RosterPacket.Item paramItem)
  {
    EMLog.d("rosterstorage", "[skip]roster storage uplodateLocalEntry:" + paramItem);
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.RosterStorageImpl
 * JD-Core Version:    0.6.1
 */